package com.dpiinc.TechPage.pen.activity

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.widget.SeekBar
import com.dpiinc.TechPage.BaseActivity
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.room.PenDaoUtils
import com.dpiinc.TechPage.widget.CommDialog
import com.beiji.lib.pen.PenManager
import com.beiji.lib.pen.cache.PenCacheManager
import com.beiji.lib.pen.model.DotUnit
import com.beiji.lib.pen.ui.view.StrokeView
import kotlinx.android.synthetic.main.activity_pen_main.*

class PenMainActivity : BaseActivity() {

    var isCacheOn = false
    var editMode = StrokeView.EditMode.STROKE

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pen_main)
        PenCacheManager.instance.init(this, "10001")
        btn_connect.setOnClickListener {
            PenManager.instance.disConnect()
            val lastPenAddress = PenManager.instance.getLastConnectedPenAddress()
            if (lastPenAddress.isNullOrEmpty()) {
                startActivity(Intent(this, ConnectPenActivity::class.java))
            } else {
                PenManager.instance.connectPen(PenDaoUtils.getPenName(this,"",lastPenAddress!! ), lastPenAddress!!)
            }

        }
        btn_connect.setOnLongClickListener {
            startActivity(Intent(this, ConnectPenActivity::class.java))
            true
        }
        btn_cache.setOnClickListener {
            if (isCacheOn) {
                stroke_view.endCache()
                btn_cache.text = "start cache"
                isCacheOn = false
            } else {
                stroke_view.startCache("0616")
                btn_cache.text = "end cache"
                isCacheOn = true
            }
        }
        btn_draw_type.setOnClickListener {
            if (editMode == StrokeView.EditMode.STROKE) {
                editMode = StrokeView.EditMode.ERASER
                btn_draw_type.setText("stroke")
            } else {
                editMode = StrokeView.EditMode.STROKE
                btn_draw_type.setText("eraser")
            }
            stroke_view.setEditMode(editMode)
        }
        btn_replay.setOnClickListener {
            startActivity(Intent(this, ReplayActivity::class.java))
        }
        sb_eraser_width.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                stroke_view.setEraserRadius(60f * progress / 100)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
        PenManager.instance.registerDataCallback(penCallback)
        btn_test.setOnClickListener {
            val builder = CommDialog.Builder()
            builder.setMessage(getString(R.string.dialog_content))  // 设置内容
                    //设置左边按钮文本和点击事件
                    .setLeftButton(getString(R.string.btn_left_text_cancel), DialogInterface.OnClickListener { dialogInterface, _ ->
                        dialogInterface.dismiss()
                    })
                    // 设置右边按钮和点击事件
                    .setRightButton(getString(R.string.btn_right_text_ok), DialogInterface.OnClickListener { dialogInterface, _ ->
                        dialogInterface.dismiss()
                    })
            // 设置弹窗
            builder.build().show(supportFragmentManager)
        }
    }

    private val penCallback: (DotUnit) -> Boolean = {
        stroke_view.drawDot(it)
        true
    }

    override fun onDestroy() {
        super.onDestroy()
        PenManager.instance.unRegisterDataCallback(penCallback)
    }

}
